package xyz.yuzegod.megawalls.inventory;

import fr.minuskube.inv.ClickableItem;
import fr.minuskube.inv.SmartInventory;
import fr.minuskube.inv.content.InventoryContents;
import fr.minuskube.inv.content.InventoryProvider;
import org.bukkit.Bukkit;
import org.bukkit.Material;
import org.bukkit.Sound;
import org.bukkit.craftbukkit.v1_8_R3.entity.CraftPlayer;
import org.bukkit.entity.Player;
import org.bukkit.event.inventory.InventoryType;
import xyz.yuzegod.megawalls.MegaWalls;
import xyz.yuzegod.megawalls.classes.Classes;
import xyz.yuzegod.megawalls.classes.ClassesManager;
import xyz.yuzegod.megawalls.classes.ClassesSkin;
import xyz.yuzegod.megawalls.game.GamePlayer;
import xyz.yuzegod.megawalls.stats.KitStatsContainer;
import xyz.yuzegod.megawalls.util.ItemBuilder;
import xyz.yuzegod.megawalls.util.PlayerUtils;

import java.util.ArrayList;
import java.util.Iterator;
import java.util.List;

public class ClassesSelector {
     public static SmartInventory build() {
          SmartInventory.Builder builder = SmartInventory.builder(MegaWalls.getInstance().getInventoryManager());
          builder.title("职业选择器");
          builder.type(InventoryType.CHEST);
          builder.size(6, 9);
          builder.closeable(true);
          builder.provider(new InventoryProvider() {
               public void init(Player player, InventoryContents contents) {
                    GamePlayer gamePlayer = GamePlayer.get(player.getUniqueId());
                    int row = 1;
                    int column = 1;
                    List classesList = ClassesManager.sort(gamePlayer.getPlayerStats().unlockedClasses());
                    Iterator var7 = classesList.iterator();

                    while(var7.hasNext()) {
                         Classes classes = (Classes)var7.next();
                         if (gamePlayer.getPlayerStats().isUnlocked(classes)) {
                              KitStatsContainer kitStats = gamePlayer.getPlayerStats().getKitStats(classes);
                              ItemBuilder itemBuilder = (new ItemBuilder(classes.getIconType(), 1, classes.getIconData())).setDisplayName("§a" + classes.getDisplayName() + ClassesSelector.getMasterStar(kitStats.getLevel()));
                              List lore = new ArrayList();
                              lore.add("§8" + classes.getClassesType().getName() + "职业");
                              lore.add("§7职业定位:" + classes.getOrientations()[0].getText() + " " + classes.getOrientations()[1].getText());
                              lore.add("§7难度:" + classes.getDifficulty().getText());
                              lore.add(" ");
                              lore.add("§e技能 §8- §7" + classes.getMainSkill().getName());
                              lore.addAll(classes.getMainSkill().getInfo(kitStats.getSkillLevel()));
                              lore.add(" ");
                              lore.add("§7升级:" + kitStats.upgradePercent());
                              lore.add("§7末影箱:§a" + kitStats.getEnderChest() + "行");
                              lore.add(" ");
                              if (gamePlayer.getPlayerStats().getSelected().equals(classes)) {
                                   lore.add("§a已选择！");
                              } else {
                                   lore.add("§e点击选择！");
                              }

                              itemBuilder.setLore((List)lore);
                              contents.set(row, column, ClickableItem.of(itemBuilder.build(), (e) -> {
                                   e.setCancelled(true);
                                   if (gamePlayer.getPlayerStats().getSelected().equals(classes)) {
                                        gamePlayer.sendMessage("§a你已经选择了该职业！");
                                   } else {
                                        gamePlayer.sendMessage("§a你选择了§e" + classes.getDisplayName() + "§a职业！");
                                        gamePlayer.playSound(Sound.CLICK, 1.0F, 1.0F);
                                        gamePlayer.getPlayerStats().setSelected(classes.getName());
                                        Bukkit.getScheduler().runTaskLaterAsynchronously(MegaWalls.getInstance(), new Runnable() {
                                             public void run() {
                                                  ClassesSkin defaultSkin = classes.getDefaultSkin();
                                                  PlayerUtils.skinChange((CraftPlayer)player, defaultSkin.getValue(), defaultSkin.getSignature());
                                             }
                                        }, 1L);
                                   }
                              }));
                              if (column + 1 > 7) {
                                   ++row;
                                   column = 1;
                              } else {
                                   ++column;
                              }
                         }
                    }

                    contents.set(5, 4, ClickableItem.of((new ItemBuilder(Material.BARRIER)).setDisplayName("§c关闭").build(), (e) -> {
                         player.closeInventory();
                    }));
               }

               public void update(Player player, InventoryContents contents) {
               }
          });
          return builder.build();
     }

     public static String getMasterStar(int level) {
          if (level < 2) {
               return "";
          } else {
               StringBuffer sb = new StringBuffer(" §6");

               for(int i = 2; i <= level; ++i) {
                    sb.append("✫");
               }

               return sb.toString();
          }
     }
}
